package com.isman.placeslist;

import com.google.gson.Gson;
import com.isman.placeslist.apiobject.Status.Location;
import com.isman.placeslist.util.Util;
import com.isman.placeslist.ws.AsyncRunnerHeadler;
import com.isman.placeslist.ws.ServiceProvider;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.ScrollView;
import android.widget.TextView;

public class TabPlaceDetail extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab_place_detail);
		mLocationId = getIntent().getLongExtra(PagePlace.LOCATION_ID, 0);

		init();
		initDate();

	}

	protected void init() {
		vtxtName = (TextView) findViewById(R.id.lbl_pl_detail_name);
		vtxtPlaceName = (TextView) findViewById(R.id.lbl_pl_detail_place);
		vtxtCheckInCount = (TextView) findViewById(R.id.lbl_pl_detail_checkinCount);
		vtxtLikeCount = (TextView) findViewById(R.id.lbl_pl_detail_likeCount);
		vtxtPictureCount = (TextView) findViewById(R.id.lbl_pl_detail_pictureCount);
		vimgLogo = (ImageView) findViewById(R.id.img_pl_detail_logo);
		vimgMap = (ImageView) findViewById(R.id.img_pl_detal_map);
		vbtnCheckIn = (Button) findViewById(R.id.btn_pl_detail_checkin);
		vbtnShared = (Button) findViewById(R.id.btn_pl_detail_shared);
		vbtnCapture = (Button) findViewById(R.id.btn_pl_detail_capture);
		vsvBody = (ScrollView) findViewById(R.id.ScrollView1);
		vpbLoad = (ProgressBar) findViewById(R.id.progressBar1);

		vbtnCapture.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mCapture();
			}
		});
		vbtnCheckIn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mCheckIn();
			}
		});
		vbtnShared.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mShared();
			}
		});
	}

	protected void mShared() {
		// TODO Auto-generated method stub

	}

	protected void mCheckIn() {
		Intent intent = new Intent(this, PageNewStatus.class);
		intent.putExtra(PageNewStatus.POST_EVENT, PageNewStatus.CHECKIN);
		intent.putExtra(PageNewStatus.LOCATION_INFO, mLocation);
		startActivityForResult(intent, 0);
	}

	protected void mCapture() {
		Intent intent = new Intent(this, PageNewStatus.class);
		intent.putExtra(PageNewStatus.POST_EVENT, PageNewStatus.CAPTURE);
		intent.putExtra(PageNewStatus.LOCATION_INFO, mLocation);
		startActivityForResult(intent, 0);
	}

	protected void initDate() {
		vsvBody.setVisibility(View.GONE);
		vpbLoad.setVisibility(View.VISIBLE);

		ServiceProvider provider = new ServiceProvider(this);
		provider.GetLocation(mLocationId, new AsyncRunnerHeadler() {
			@Override
			public void onError(int statusCode, String json) {
			}

			@Override
			public void onConnectingError(String message) {

			}

			@Override
			public void onComplete(String json) {
				Log.e("TAG", json);
				mLocation = new Gson().fromJson(json, Location.class);

				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						StringBuilder sb = new StringBuilder();
						if (mLocation.getMap().getSubLocality() != null) {
							sb.append(mLocation.getMap().getSubLocality() + " ");
						}
						if (mLocation.getMap().getLocality() != null) {
							sb.append(mLocation.getMap().getLocality() + " ");
						}
						if (mLocation.getMap().getAdminArea() != null) {
							sb.append(mLocation.getMap().getAdminArea() + " ");
						}
						if (mLocation.getMap().getCountryName() != null) {
							sb.append("\n"
									+ mLocation.getMap().getCountryName() + " ");
						}
						if (mLocation.getMap().getCountryCode() != null) {
							sb.append(mLocation.getMap().getCountryCode());
						}

						vtxtPlaceName.setText(sb);
						vtxtName.setText(mLocation.getName());
						vtxtCheckInCount.setText(mLocation.getInfo()
								.getCheckInCount() + "");
						vtxtLikeCount.setText(mLocation.getInfo()
								.getLikeCount() + "");

						vsvBody.setVisibility(View.VISIBLE);
						vpbLoad.setVisibility(View.GONE);
					}
				});
				mLoadImage();
			}

			@Override
			public void onAuthenticate(String json) {
			}
		});
	}

	protected void mLoadImage() {
		int with = getWindowManager().getDefaultDisplay().getWidth();
		final String mLink = "http://maps.googleapis.com/maps/api/staticmap?"
				+ "center="
				+ mLocation.getMap().getLatitude()
				+ ","
				+ mLocation.getMap().getLongitude()
				+ "&zoom=12&size="
				+ with
				+ "x200"
				+ "&sensor=false"
				+
				// "&markers=icon:http://placeslist.com/pin.png"+
				// "&markers=color:red%7Clabel:G"+
				"&markers=icon:http://files.softicons.com/download/toolbar-icons/black-wireframe-toolbar-icons-by-gentleface/png/48/pin_map.png"
				+ "%7C" + mLocation.getMap().getLatitude() + ","
				+ mLocation.getMap().getLongitude();

		Util.getBitmap(mLocation.getPicture(), this, vimgLogo);
		Util.getBitmap(mLink, this, vimgMap);
	}

	private TextView vtxtName;
	private TextView vtxtPlaceName;
	private TextView vtxtCheckInCount;
	private TextView vtxtLikeCount;
	private TextView vtxtPictureCount;
	private ImageView vimgLogo;
	private ImageView vimgMap;
	private Button vbtnCheckIn;
	private Button vbtnShared;
	private Button vbtnCapture;
	private ScrollView vsvBody;
	private ProgressBar vpbLoad;

	private long mLocationId;
	private Location mLocation;
}
